//
//  Action.m
//  addActionForm
//
//  Created by user on 11.04.11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Action.h"


@implementation Action

@synthesize actionsObject;
@synthesize commentList;
@synthesize resourceList;
@synthesize historyList;
@synthesize actionPriority;
@synthesize isNew;
@synthesize changed;

- (id) init{
	self = [super init];
	commentList = [[NSMutableArray alloc] init];
	resourceList = [[NSMutableArray alloc] init];
	actionsObject = [[Actions alloc] init];	
	actionPriority = nil;
	isNew = YES;
	changed = YES;
	[actionsObject setVDateTime_Created : [Library getCurrentTime]];
	[actionsObject setVDescription:@"Add a description here..."];
	return self;
}

- (id) initWithProject : (Project *) project{
	[self init];
	[actionsObject setVId_Projects:[project getProjectId]];
	return self;
}


- (id) initWithActions : (Actions *) actions {
	/*Alterar esta inicializacao*/
	[self init];
	actionsObject = actions;
	isNew = NO;
	[self loadResources];
	[self loadPriority];
	return self;
}


-(void) addComment : (Comment *) comment{
	History *history = [[History alloc] initWithAction:self];
	[history setHistorydescription : [[NSString alloc] initWithFormat:__GENERICCOMMENTADDED,  @"Action"]];
	[commentList addObject:comment];
}

-(void) addResource : (Resource *) resource{
	History *history = [[History alloc] initWithAction:self];
	[history setHistorydescription : [[NSString alloc] initWithFormat:__GENERICRESOURCEADDED, [resource getName], @"Action"]];
	[historyList addObject:history];
   [resourceList addObject:resource];
}

- (NSString *) getActionDescription{
	return [actionsObject vDescription];
}

- (void) setActionDescription : (NSString *) descript{
	History *history = [[History alloc] initWithAction:self];
	[history setHistorydescription : [[NSString alloc] initWithFormat:__ACTIONSDESCRIPTIONCHANGEDTO]];
	[historyList addObject:history];
	changed = YES;
	[actionsObject setVDescription: descript];
}


- (void) setFinishDate : (double) time{
	History *history = [[History alloc] initWithAction:self];
	[history setHistorydescription : [[NSString alloc] initWithFormat:__ACTIONSFINISHDATECHANGEDTO, [Library getLocalizedDateString:time]]];
	[historyList addObject:history];
	changed = YES;
	[actionsObject setVFinish : time ];
}
   
- (void) setBaselineFinishDate : (double) time{
	History *history = [[History alloc] initWithAction:self];
	[history setHistorydescription : [[NSString alloc] initWithFormat:__ACTIONSBASELINEFINISHDATECHANGEDTO, [Library getLocalizedDateString:time]]];
	[historyList addObject:history];
	changed = YES;
	[actionsObject setVBaseline_Finish: time];
}


- (SEL) getSetFinishSelector{
	return @selector(setFinishDate:);
}


- (SEL) getSetBaselineFinishSelector{
	return @selector(setBaselineFinishDate:);
}


- (void) loadPriority{
	DataContext *context = [[DataContext alloc]init];
	NSMutableArray * colPriorities = [[NSMutableArray alloc] init];
	[context getFromQueryToArrayPriorities:[actionsObject getSelectStringForPriorities] : colPriorities];
	if([colPriorities count]>0){
		actionPriority = [[Priority alloc] initWithPriorities:[colPriorities objectAtIndex:0]];
		[colPriorities removeAllObjects];
	}else {
		actionPriority = nil;
	}
	[colPriorities dealloc];
	[context dealloc];
}


- (void) loadComments{
}

- (void) loadResources{
	DataContext *context = [[DataContext alloc]init];
	[context getFromQueryToArrayResources:[actionsObject getSelectStringForResources] : [actionsObject colResources]];
	[context dealloc];
	for(Resources *p in [actionsObject colResources])
	{
		Resource *resource = [[Resource alloc] initWithResources:p];
		[resourceList addObject:resource];
	}
}

- (void) loadHistory{
}

- (void) dealloc{
    [actionsObject dealloc];
	actionsObject = nil;
	[super dealloc];
	//Alterar aqui para remover arrays
	/*
	@property (readonly)  NSMutableArray *commentList;
	@property (readonly)  NSMutableArray *resourceList;
	@property (readonly)  NSMutableArray *historyList;
	 */
}

- (NSString *) getOwner{
	return @"You";
	return [[resourceList objectAtIndex:0] getName]; 
}

- (NSString *) getInsertString{
	return [actionsObject getInsertStringForSelf];
}

- (NSString *) getUpdateString{
	return [actionsObject getUpdateStringForSelf];
}

- (double) getDateCreated{
	return [actionsObject vDateTime_Created];
}

- (double) getBaseLineFinish{
	return [actionsObject vBaseline_Finish];
}

- (double) getFinish{
	return [actionsObject vFinish];
}


- (int) getActionId{
	return [actionsObject vId_Actions];
}

- (NSString *) getPriority{
	return [actionPriority getPriorityDescription];
}




- (void) unloadComments{/*
	int conta = [actionList count];
	for(int i = 0; i < conta; i++)
	{
		Action * action = [actionList objectAtIndex: 0];
		[actionList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colActions] removeObjectAtIndex:0];
		
		
		
		[action dealloc];
		action = nil;
	}*/
}

- (void) unloadResources{/*
	int conta = [actionList count];
	for(int i = 0; i < conta; i++)
	{
		Action * action = [actionList objectAtIndex: 0];
		[actionList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colActions] removeObjectAtIndex:0];
		
		
		
		[action dealloc];
		action = nil;
	}*/
}


- (void) unloadHistory{/*
	int conta = [actionList count];
	for(int i = 0; i < conta; i++)
	{
		Action * action = [actionList objectAtIndex: 0];
		[actionList removeObjectAtIndex:0];
		
		//Clean the array of objects
		[[projects colActions] removeObjectAtIndex:0];
		
		
		
		[action dealloc];
		action = nil;
	}*/
}




@end
